Create function to quote string?

Hi, how I can create function using DXl, which:

string quote(string s)

will return me the quoted s representation. For Example:

 

string a = "asd"

print quote(a)

---> "asd"

string b= "     boo-boo-boo      "

print quote(b)

--->boo-boo-boo

 

so to quote and to trim, white spaces. 


Dinio - Wed Dec 16 09:48:06 EST 2015

Re: Create function to quote string?
Mathias Mamsch - Wed Dec 16 11:08:58 EST 2015

Normally you employ regexp or work on character level for these kinds of functions. There are lots of examples for searching, splitting, trimming, replacing, etc. stuff in strings on the forum. You should take a look and come back with specific problems. Regards, Mathias

Re: Create function to quote string?
Dinio - Fri Dec 18 10:20:09 EST 2015

I try this:

 

const string SINGLE_QUOTE = "\'";
const string DOUBLE_QUOTE = "\"";

// The pourpose of this function is to remove the leading and the following whitespaces (space,\\n,\\t,\\r)
//in the input string "s"
string trim(string s) {

   int first = 0
   int last = ( length s ) - 1
   while ( last > 0 && isspace( s[ last ] ) ) last--
   while ( isspace( s[ first ] ) && first < last ) first++
   if ( first == last && isspace s[first] ) return ""
   return s[ first:last ]
}

string quote(string str) { 
    string result
    if(! null str) {
        result = DOUBLE_QUOTE str DOUBLE_QUOTE
    }
    return result
}

string quoteArgument(string str) {
    string trimmed = trim(str)
    return      quote(trimmed)
}


//print trim("     asd asd ")
//print quote("     asd asd ")
print quoteArgument("     asd asda ")

 

But when try the quoteArgument function something strange happened:

The result is:

"A
"

On the second execution was:

"1
"

I think this is some strange feature of DXL.

Re: Create function to quote string?
Mathias Mamsch - Fri Dec 18 10:35:23 EST 2015

Dinio - Fri Dec 18 10:20:09 EST 2015

I try this:

 

const string SINGLE_QUOTE = "\'";
const string DOUBLE_QUOTE = "\"";

// The pourpose of this function is to remove the leading and the following whitespaces (space,\\n,\\t,\\r)
//in the input string "s"
string trim(string s) {

   int first = 0
   int last = ( length s ) - 1
   while ( last > 0 && isspace( s[ last ] ) ) last--
   while ( isspace( s[ first ] ) && first < last ) first++
   if ( first == last && isspace s[first] ) return ""
   return s[ first:last ]
}

string quote(string str) { 
    string result
    if(! null str) {
        result = DOUBLE_QUOTE str DOUBLE_QUOTE
    }
    return result
}

string quoteArgument(string str) {
    string trimmed = trim(str)
    return      quote(trimmed)
}


//print trim("     asd asd ")
//print quote("     asd asd ")
print quoteArgument("     asd asda ")

 

But when try the quoteArgument function something strange happened:

The result is:

"A
"

On the second execution was:

"1
"

I think this is some strange feature of DXL.

I think the strange feature you are talking about is:

print quoteArgument("  asd asd ") "\n"

This is interpreted in DXL as

print quoteArgument ("  asd asd " "\n" )

which will quote the newline. Functions with one parameter in DXL behave a bit strange, since you can leave out the parenthesis.

print quoteArgument "Hallo"

This makes the syntax ambiguous, thats why I tend to write function calls with one argument inside an expression always like this:

print ( quoteArgument "  asd asd ") "\n"

Hope that helps. Regards, Mathias

Re: Create function to quote string?
Dinio - Mon Jan 04 10:53:56 EST 2016

Mathias Mamsch - Fri Dec 18 10:35:23 EST 2015

I think the strange feature you are talking about is:

print quoteArgument("  asd asd ") "\n"

This is interpreted in DXL as

print quoteArgument ("  asd asd " "\n" )

which will quote the newline. Functions with one parameter in DXL behave a bit strange, since you can leave out the parenthesis.

print quoteArgument "Hallo"

This makes the syntax ambiguous, thats why I tend to write function calls with one argument inside an expression always like this:

print ( quoteArgument "  asd asd ") "\n"

Hope that helps. Regards, Mathias

I try

print ( quoteArgument "  asd asd ") "\n"

 

but the result is

 

"=
!Ce"

I think the problem comes from the nested think. 

trim(str)

in the quoteArgument print wrong thing when called in quoteArgument but if trim(str) is called separately everything is OK.

Re: Create function to quote string?
Mike.Scharnow - Mon Jan 04 13:16:35 EST 2016

Dinio - Mon Jan 04 10:53:56 EST 2016

I try

print ( quoteArgument "  asd asd ") "\n"

 

but the result is

 

"=
!Ce"

I think the problem comes from the nested think. 

trim(str)

in the quoteArgument print wrong thing when called in quoteArgument but if trim(str) is called separately everything is OK.

I copied your exact code beginning with "const string" and ending with "print quoteArgument("     asd asda ") " and added three times

print ( quoteArgument "  asd asd ") "\n"
 

The result is, as expected, exactly

"asd asda""asd asd"
"asd asd"
"asd asd"

Confirmed with DOORS 9.2 and DOORS 9.6.1.4

Is this really the complete code or are there other parts of the code which interfere with your result?

 

Re: Create function to quote string?
Dinio - Tue Jan 05 02:56:35 EST 2016

Mike.Scharnow - Mon Jan 04 13:16:35 EST 2016

I copied your exact code beginning with "const string" and ending with "print quoteArgument("     asd asda ") " and added three times

print ( quoteArgument "  asd asd ") "\n"
 

The result is, as expected, exactly

"asd asda""asd asd"
"asd asd"
"asd asd"

Confirmed with DOORS 9.2 and DOORS 9.6.1.4

Is this really the complete code or are there other parts of the code which interfere with your result?

 

Only this code I have in the script.Doors 9.5.1.2. Can be from some switch of the rich client?

 

const string SINGLE_QUOTE = "\'";
const string DOUBLE_QUOTE = "\"";
// The pourpose of this function is to remove the leading and the following whitespaces (space,\\n,\\t,\\r)
//in the input string "s"
string trim(string s) {
   int first = 0
   int last = ( length s ) - 1
   while ( last > 0 && isspace( s[ last ] ) ) last--
   while ( isspace( s[ first ] ) && first < last ) first++
   if ( first == last && isspace s[first] ) return ""
   return s[ first:last ]
}
string quote(string str) { 
    string result
    if(! null str) {
        result = DOUBLE_QUOTE str DOUBLE_QUOTE
    }
    return result
}
string quoteArgument(string str) {
    string trimmed = trim(str)
    return      quote(trimmed)
}

//print trim("     asd asd ")
//print quote("     asd asd ")
print ( quoteArgument "  asd asd ") "\n"

The result is still

"9
!Ce"

 

Re: Create function to quote string?
Mathias Mamsch - Tue Jan 05 06:23:15 EST 2016

Dinio - Tue Jan 05 02:56:35 EST 2016

Only this code I have in the script.Doors 9.5.1.2. Can be from some switch of the rich client?

 

const string SINGLE_QUOTE = "\'";
const string DOUBLE_QUOTE = "\"";
// The pourpose of this function is to remove the leading and the following whitespaces (space,\\n,\\t,\\r)
//in the input string "s"
string trim(string s) {
   int first = 0
   int last = ( length s ) - 1
   while ( last > 0 && isspace( s[ last ] ) ) last--
   while ( isspace( s[ first ] ) && first < last ) first++
   if ( first == last && isspace s[first] ) return ""
   return s[ first:last ]
}
string quote(string str) { 
    string result
    if(! null str) {
        result = DOUBLE_QUOTE str DOUBLE_QUOTE
    }
    return result
}
string quoteArgument(string str) {
    string trimmed = trim(str)
    return      quote(trimmed)
}

//print trim("     asd asd ")
//print quote("     asd asd ")
print ( quoteArgument "  asd asd ") "\n"

The result is still

"9
!Ce"

 

It could be a namespace conflict with another function. Try the following:

0. Restart your DOORS client (you did that right?)

1. try

string s = quoteArgument " asd asd "

print s "\n"     // should resolve the namespace conflict by explicitly telling DOORS which function to call

2. Use the original code but rename the "trim" function to something else. That should also resolve a namespace conflict.

I remember something about a "trim" function, try this to make sure you are not having a problem with a top level function somehow taking precedence inside the expression. Regards, Mathias

Re: Create function to quote string?
Dinio - Tue Jan 05 07:11:32 EST 2016

Mathias Mamsch - Tue Jan 05 06:23:15 EST 2016

It could be a namespace conflict with another function. Try the following:

0. Restart your DOORS client (you did that right?)

1. try

string s = quoteArgument " asd asd "

print s "\n"     // should resolve the namespace conflict by explicitly telling DOORS which function to call

2. Use the original code but rename the "trim" function to something else. That should also resolve a namespace conflict.

I remember something about a "trim" function, try this to make sure you are not having a problem with a top level function somehow taking precedence inside the expression. Regards, Mathias

I still can not explain to my self but obviously trim is occupied name. If I change it to trim2 everything works.

I am much obliged to you for your patience boys during the recent difficulties.

 

 

Re: Create function to quote string?
Mike.Scharnow - Tue Jan 05 07:42:02 EST 2016

Dinio - Tue Jan 05 07:11:32 EST 2016

I still can not explain to my self but obviously trim is occupied name. If I change it to trim2 everything works.

I am much obliged to you for your patience boys during the recent difficulties.

 

 

I found the thread about the ominous trim function: https://www.ibm.com/developerworks/community/forums/html/topic?id=efdabdc1-deab-4c63-acb2-037677a3ceeb&ps=25

As usual for DOORS: interesting and strange...